<?php
	session_start();
	if(!isset($_SESSION["jara_admin"])) {
		header("Location: ../login.php");
		exit;
	}
	if($_SESSION["jara_permissions"][2] == 0) {	
		header("Location: error.php");
		exit;
	}
	require_once("../include/templating_fns.php");
	require_once("../include/db_fns.php");
	require_once("../include/auth_fns.php");
	jara_page_start("Add Category - Administrator CP");
	if(isset($_POST["category_action"])) {
		try {
			$title = $_POST["title"];
			$name = strtolower(preg_replace("/[^a-zA-Z0-9]/", "-", $_POST["title"]));
			if(!get_magic_quotes_gpc()) {
				$title = addslashes($title);
				$name = addslashes($name);
			}
			$result = jara_db_query("insert into jara_categories values(NULL, '$title' ,'$name')");
			if($result == false) {
				throw new JaraGeneralException("Could not create the category $title.");
			}
			else {
				echo "<p>The category <strong>$title</strong> has been created.<br /><a href=\"categories.php\">Return to Categories</a><br /><a href=\"add_category.php\">Add Another</a></p>";
				jara_page_end();
				exit;
			}
		}
		catch(JaraDatabaseException $ex) {
?>
	<p>
		A database error occured during the process of creating this page.
	</p>
	<p>
		Error type: <?php echo (($ex->getSqlStage() == 1) ? "MySQL connection error" : "MySQL query error"); ?><br />
		Error message: &quot;<?php echo $ex->getSqlMsg(); ?>&quot; [code <?php echo $ex->getSqlCode(); ?>]
	</p>
<?php
			jara_page_end();
		}
		catch(JaraGeneralException $ex) {
?>
	<p>
		A general error occured during the process of creating this page.
	</p>
	<p>
		Error message: &quot;<?php echo $ex->getMessage(); ?>&quot;
	</p>
<?php
			jara_page_end();
		}
		exit;
	}
?>
<form action="add_category.php" method="post">
<input type="hidden" name="category_action" id="category_action" value="create" />
<h3>Add a Category</h3>
<p>
Title: <input type="text" name="title" id="title" />
</p>
<p>
<input type="submit" value="Save" />
</p>
</form>
<?php
	jara_page_end();
?>